/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "cylinder.h"

#include "math/ray.h"
#include "material/object_material.h"

#include <limits>
#include <cmath>
#include <algorithm>

namespace Sirat {

namespace Object {

Cylinder::Cylinder(Material::Object_Material* _object_material,const Math::Vector& _center,const Math::Vector& _direction, const double _radius,const double _height) : Object(_object_material), center(_center) , direction(_direction.normalize()) , radius(_radius) , height(_height) { }

Cylinder::Cylinder() : Object(), center(Math::Vector(0.0,0.0,0.0)) , direction(Math::Vector(0.0,1.0,0.0)) , radius(1.0) , height(1.0) { }

Cylinder::Cylinder(const Cylinder& _cylinder) : Object(_cylinder) {
	center = _cylinder.center;
	direction = _cylinder.direction;
	radius = _cylinder.radius;
	height = _cylinder.height;	
}

Cylinder::~Cylinder() { }

Math::Vector Cylinder::get_center() const { return center; }

Math::Vector Cylinder::get_direction() const { return direction; }

double Cylinder::get_radius() const { return radius; }

double Cylinder::get_height() const { return height; }

Math::Vector Cylinder::get_normal(const Math::Vector& _point) const { 
	Math::Vector point = _point - center; //Change coordinates system to cylinder coordinate
	double relative_height = point.dot_product(direction);
	
	Math::Vector point_height = direction*relative_height;
	Math::Vector return_normal = (point - point_height);
	return return_normal.normalize();
}

void Cylinder::set_center(const Math::Vector& _center) { center = _center; }

void Cylinder::set_direction(const Math::Vector& _direction) { direction = _direction.normalize(); }

void Cylinder::set_radius(const double _radius) { radius = _radius; }

void Cylinder::set_height(const double _height) { height = _height; }

tuple<double,Math::Vector,Object*> Cylinder::intersection(const Math::Ray& _ray) {
	std::numeric_limits<double> double_limit;
	const double infinity = double_limit.infinity();
	//const double epsilon = double_limit.epsilon();
	const double epsilon = 0.000001;
	Math::Vector intersection_point;

	Math::Vector jc = direction;
	Math::Vector j(0.0,1.0,0.0);
	if(std::fabs(std::fabs(j.dot_product(jc)) - 1.0) < epsilon) j = Math::Vector(1.0,0.0,0.0);

	Math::Vector kc = j.cross_product(jc).normalize();
	Math::Vector ic = jc.cross_product(kc);
	Math::Vector oc = center;
	Math::Vector o = _ray.get_origin();
	Math::Vector orel = o - oc;
	Math::Vector d = _ray.get_direction();
	
	double R = radius;

	double ic_or = ic.dot_product(orel);
	double ic_d = ic.dot_product(d);

/*	double jc_or = jc.dot_product(orel);
	double jc_d = jc.dot_product(d);*/

	double kc_or = kc.dot_product(orel);
	double kc_d = kc.dot_product(d);

	double A = ic_d*ic_d + kc_d*kc_d;
	double B = 2.0*ic_d*ic_or + 2.0*kc_d*kc_or;
	double C =  ic_or*ic_or + kc_or*kc_or -R*R;

	double delta = B*B - 4.0*A*C;

	double t1,t2,distance = infinity;

	if(std::fabs(A) < epsilon) return make_tuple(infinity,intersection_point,this);

	if(delta > 0.0)	{
		double root_delta = std::sqrt(delta);

		double t_1 = (-B + root_delta)/(2.0*A);
		double t_2 = (-B - root_delta)/(2.0*A);

		double smallest = std::min(t_1,t_2);
		double bigger = std::max(t_1,t_2);

		if(smallest < epsilon) {
			t1 = bigger;
			t2 = t1;
		}
		else {
			t1 = smallest;
			t2 = bigger;
		}
	} else if(std::fabs(delta) <= epsilon) {
		t1 = (-B)/(2.0*A);
		t2 = t1;
	} else return make_tuple(infinity,intersection_point, this);
	
	if(t1 > epsilon) {
		distance = t1;
		Math::Vector point_vector = _ray.get_origin() + _ray.get_direction()*t1 -center; 

		double relative_height = point_vector.dot_product(direction);

		if(relative_height < 0.0 || relative_height > height) {
			distance = t2;
			point_vector = _ray.get_origin() + _ray.get_direction()*t2	-center; 

			relative_height = point_vector.dot_product(direction);

			if(relative_height < 0.0 || relative_height > height) return make_tuple(infinity,point_vector,this);
		}

		return make_tuple(distance,point_vector +center,this);
	} else return make_tuple(infinity,intersection_point, this);
}

tuple<double,double> Cylinder::get_u_v(const Math::Vector& _point) const {
	
	/**
	 * Cylindrical Coordinates 
	 * First change coordinates to a Cylinder based on XZ plane
	 * And "Viva!"
	 */
	const double epsilon = 0.000001;
	Math::Vector jc = direction;
	Math::Vector j(0.0,1.0,0.0);
	if(std::fabs(std::fabs(j.dot_product(jc)) - 1.0) < epsilon) j = Math::Vector(1.0,0.0,0.0);

	Math::Vector kc = j.cross_product(jc).normalize();
	Math::Vector ic = jc.cross_product(kc);
	
	Math::Vector vec = (_point - center);
	
	double x = ic*vec;
	double y = jc*vec;
	double z = kc*vec;
		
	const double v = y/height;
	const double u = std::atan2(z/radius,x/radius);

	return make_tuple((u+M_PI)/(2.0*M_PI), (1.0-v));
	
}

Cylinder& Cylinder::operator=(const Cylinder& _cylinder) {
	Object::operator=(_cylinder);
	center = _cylinder.center;
	direction = _cylinder.direction;
	radius = _cylinder.radius;
	height = _cylinder.height;	
	return *this;
}

}

}
